use log::info;
use std::io::{self, Write};
use tokio::{io::AsyncWriteExt, net::TcpStream};

use crate::{cli::Cli, error::Error};

//create_topic --topic test --replication-factor 2 --partitions 3
pub async fn produce(mut stream: TcpStream, cli: &Cli) -> Result<(), Error> {
    let command = format!("{} --topic {}\n", cli.command, cli.topic);

    stream.write_all(command.as_bytes()).await?;
    info!("command: {command}");

    loop {
        io::stdout().flush()?; // 确保提示信息被立即显示

        // 读取用户输入
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;

        //println!("You entered: {}", input.trim());
        stream.write_all(input.trim().as_bytes()).await?;
    }
    //Ok(())
}
